﻿using Services.Activity.Domain.ValueObjects;
using Services.Initial.PublicProject.PublicClass.ExtensionException;
using System.Net;
using Zack.DomainCommons.Models;

namespace Services.Activity.Domain.Entities;
public record VoluntaryActivity : AggregateRootEntity
{
    public long ActivityId { get; private set; }      
    public string ActivityName { get; private set; }
    public ActivityTypes ActivityType { get; private set; }
    public bool ActivityComplete { get; private set; } = false;
    public DateTime ActivityStartTime { get; private set; }
    /// <summary>
    /// 活动预计结束时间
    /// </summary>
    public DateTime ActivityCompleteTime { get; private set; }
    public string ActivityDescription { get; private set; }
    public long OrganizerId { get; private set; }
    public string ActivityDetailAddress { get; private set; }
    //一个志愿活动允许有一张照片作为宣传照片
    public string? PhotoBase64 { get; private set; }
    public List<ActivityVolunteer> Volunteers { get; private set; } = [];
    public List<ActivityReviews> Reviews { get; private set; } = [];
   
    public void SetVolunteer(ActivityVolunteer activityVolunteer)
    {
        this.Volunteers.Add(activityVolunteer);
    }
    public void SetReviews(List<ActivityReviews> reviews)
    {
        this.Reviews.AddRange(reviews);
    }
    public void SetActivityComplete()
    {
        this.ActivityComplete = true;
        this.ActivityCompleteTime = DateTime.Now;
    }
    private VoluntaryActivity() { }
    public class Builder
    {

        private string activityName;
        private ActivityTypes activityTypes = ActivityTypes.未知;
        private DateTime activityStartTime;
        private DateTime activityCompleteTime;
        private string activityDescription;
        private long organizerId;
        private string activityDetailAddress;
        private string photoBase64;
        public void SetActivityName(string activityName)
        {
            this.activityName = activityName;
        }
        //把每个值依次使用方法赋值
        public void SetActivityType(ActivityTypes activityType)
        {
            this.activityTypes = activityType;
        }
        public void SetActivityStartTime(DateTime activityStartTime)
        {
            this.activityStartTime = activityStartTime;
        }
        public void SetActivityCompleteTime(DateTime activityCompleteTime)
        {
            this.activityCompleteTime = activityCompleteTime;
        }
        public void SetActivityDescription(string activityDescription)
        {
            this.activityDescription = activityDescription;
        }
        public void SetOrganizerId(long organizerId)
        {
            this.organizerId = organizerId;
        }
        public void SetActivityDetailAddress(string activityDetailAddress)
        {
            this.activityDetailAddress = activityDetailAddress;
        }
        public void SetActivityPhotoPath(string activityPhotoBase)
        {
            this.photoBase64 = activityPhotoBase;
        }
        public VoluntaryActivity Build()
        {
            //依次判断每个字段是否为空
            if (string.IsNullOrEmpty(this.activityName))
            {
                throw new ExceptionWithStatus(HttpStatusCode.BadRequest, "活动名不能为空");
            }
            if (string.IsNullOrEmpty(this.activityDetailAddress))
            {
                throw new ExceptionWithStatus(HttpStatusCode.BadRequest, "活动详细地址不能为空");
            }
            if (activityTypes == ActivityTypes.未知)
            {
                throw new ExceptionWithStatus(HttpStatusCode.BadRequest, "活动类型不能为空");
            }
            if (activityCompleteTime == DateTime.MinValue)
            {
                throw new ExceptionWithStatus(HttpStatusCode.BadRequest, "活动结束时间不能为空");
            }
            if (string.IsNullOrEmpty(this.activityDescription))
            {
                throw new ExceptionWithStatus(HttpStatusCode.BadRequest, "活动描述不能为空");
            }
            if (this.organizerId == long.MinValue)
            {
                throw new ExceptionWithStatus(HttpStatusCode.BadRequest, "发起人不能为空");
            }
            var activity = new VoluntaryActivity()
            {
                ActivityName = this.activityName,
                ActivityDetailAddress = this.activityDetailAddress,
                ActivityStartTime = this.activityStartTime,
                ActivityCompleteTime = this.activityCompleteTime,
                ActivityDescription = this.activityDescription,
                ActivityType = this.activityTypes,
                OrganizerId = this.organizerId,
                PhotoBase64 = this.photoBase64,
            };
            return activity;
        }
    }
}